<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-cascades"></i> 用户评论
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container v2-group">
            <!-- <div class="bulletin">
                <el-popover
                    placement="bottom"
                    trigger="hover">
                    <div class="pop-content statistics">
                        <div>
                            <el-statistic
                                    group-separator=","
                                    :precision="0"
                                    :value="statistics.allow_count"
                                    title="已审核数"
                                ></el-statistic>
                            <el-statistic
                                    group-separator=","
                                    :precision="0"
                                    :value="statistics.no_allow_count"
                                    title="未审核数"
                                ></el-statistic>
                            <el-statistic
                                    group-separator=","
                                    :precision="0"
                                    :value="statistics.no_table_count"
                                    title="不具备数据表数"
                                ></el-statistic>
                            <div @click="handleAssignNum">
                                <el-statistic
                                        group-separator=","
                                        :precision="0"
                                        :value="statistics.crawling_assign_count"
                                        title="已分配任务数"
                                    ></el-statistic>
                            </div>
                            <div @click="handleUnassignNum">
                                <el-statistic
                                        group-separator=","
                                        :precision="0"
                                        :value="statistics.crawling_no_assign_count"
                                        title="待分配任务数"
                                    ></el-statistic>
                            </div>
                        </div>
                    </div>
                    <el-row slot="reference" :gutter="20"
                        v-loading="loading"
                        >
                        <el-col :span="8">
                            <div>
                            <el-statistic
                                group-separator=","
                                :precision="0"
                                :value="statistics.all_count"
                                title="数据类别总数"
                            ></el-statistic>
                            </div>
                        </el-col>
                        <el-col :span="8">
                            <div @click="handleAssignTotal">
                            <el-statistic
                                group-separator=","
                                :precision="0"
                                decimal-separator="."
                                :value="statistics.crawling_all_count"
                                title="爬取任务数"
                            >
                            </el-statistic>
                            </div>
                        </el-col>
                        <el-col :span="8">
                            <div>
                            <el-statistic :value="statistics.table_count" title="具备数据表数">
                                <template slot="suffix">
                                <i class="el-icon-s-flag" style="color: red"></i>
                                </template>
                            </el-statistic>
                            </div>
                        </el-col>
                    </el-row>
                </el-popover>
            </div> -->
            <div class="handle-box">
                <el-select v-model="query.sift_type" placeholder="- 消息类型 -" clearable>
                    <el-option v-for="item in sysdict.feedbackType" :key="item.label" :label="item.name" :value="item.name"></el-option>
                </el-select>
                <el-input v-model="query.search_text" placeholder="关键词" class="handle-input mr10" clearable></el-input>
                <el-button type="primary" icon="el-icon-search" @click="handleSearch">查询</el-button>
                <!-- <el-button
                    type="success"
                    icon="el-icon-plus"
                    class="handle-del mr10"
                    @click="handleAdd"
                >新增类别</el-button>
                <el-tooltip class="item" effect="dark" content="批量删除" placement="bottom">
                    <el-button type="danger" icon="el-icon-delete" circle @click="handleBatchDelete"></el-button>
                </el-tooltip> -->
            </div>
            <el-table
                :data="tableData"
                border
                class="table"
                ref="multipleTable"
                header-cell-class-name="table-header"
                @selection-change="handleSelectionChange"
                stripe
                v-loading="loading"
                @sort-change="changedSort"
            >
                <!-- <el-table-column type="selection" width="55" align="center"></el-table-column> -->
                <!-- <el-table-column prop="id" label="id" width="55"></el-table-column> -->
                <el-table-column prop="type" label="消息类型" width="120" sortable></el-table-column>
                <el-table-column prop="description" label="反馈内容" show-overflow-tooltip></el-table-column>
                <!-- <el-table-column prop="username" label="反馈用户" width="120" sortable></el-table-column> -->
                <el-table-column prop="updateTime" label="提交时间" width="180" sortable></el-table-column>
                <!-- <el-table-column prop="update_time" width="120" label="更新时间" sortable>
                    <template slot-scope="scope">
                        <el-tag
                            :type="scope.row.is_table===true?'':'warning'"
                        >{{scope.row.is_table ? '存在' : '不存在'}}</el-tag>
                    </template>
                </el-table-column> -->
                <el-table-column label="操作" width="90px">
                    <template slot-scope="scope">
                        <el-button
                            type="text"
                            icon="el-icon-delete"
                            class="red"
                            @click="handleDelete(scope.$index, scope.row)"
                        >删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                    background
                    layout="total, sizes, prev, pager, next, jumper"
                    :current-page="query.page"
                    :page-size="query.page_size"
                    :total="pageTotal"
                    @current-change="handlePageChange"
                    :page-sizes="[10, 20, 30, 40, 50, 100]"
                    @size-change="handleSizeChange"
                ></el-pagination>
            </div>
        </div>

        <!-- 批量表格弹出框 -->
        <el-dialog title="批量设置表格" :visible.sync="tableBatchVisible" width="30%">
            <el-form ref="entityEdit" :model="form" :rules="rulesEntityEdit" label-width="70px">
                <el-form-item label="审核状态" prop="is_table">
                    <el-switch
                        v-model="form.is_table"
                        active-text="存在"
                        inactive-text="不存在">
                    </el-switch>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="tableBatchVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveBatchTable">确 定</el-button>
            </span>
        </el-dialog>

        <!-- 表格弹出框 -->
        <el-dialog title="设置表格" :visible.sync="tableVisible" width="30%">
            <el-form ref="entityEdit" :model="form" :rules="rulesEntityEdit" label-width="70px">
                <el-form-item label="审核状态" prop="is_table">
                    <el-switch
                        v-model="form.is_table"
                        active-text="存在"
                        inactive-text="不存在">
                    </el-switch>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="tableVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveTable">确 定</el-button>
            </span>
        </el-dialog>

        <!-- 批量审核弹出框 -->
        <el-dialog title="审核" :visible.sync="allowBatchVisible" width="30%">
            <el-form ref="entityEdit" :model="form" :rules="rulesEntityEdit" label-width="70px">
                <el-form-item label="审核状态" prop="is_allow">
                    <el-switch
                        v-model="form.is_allow"
                        active-text="通过"
                        inactive-text="不通过">
                    </el-switch>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="allowBatchVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveBatchAllow">确 定</el-button>
            </span>
        </el-dialog>

        <!-- 审核弹出框 -->
        <el-dialog title="审核" :visible.sync="allowVisible" width="30%">
            <el-form ref="entityEdit" :model="form" :rules="rulesEntityEdit" label-width="70px">
                <el-form-item label="审核状态" prop="is_allow">
                    <el-switch
                        v-model="form.is_allow"
                        active-text="通过"
                        inactive-text="不通过">
                    </el-switch>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="allowVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveAllow">确 定</el-button>
            </span>
        </el-dialog>

        <!-- 编辑弹出框 -->
        <el-dialog title="编辑" :visible.sync="editVisible" width="30%">
            <el-form ref="entityEdit" :model="form" :rules="rulesEntityEdit" label-width="70px">
                <el-form-item label="名称" prop="name">
                    <el-input v-model="form.name" disabled></el-input>
                </el-form-item>
                <el-form-item label="含义" prop="meaning">
                    <el-input v-model="form.meaning"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="editVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveEdit">确 定</el-button>
            </span>
        </el-dialog>

        <!-- 新增弹出框 -->
        <el-dialog title="新增" :visible.sync="addVisible" width="530px">
            <el-form ref="entityAdd" :model="form" :rules="rulesEntityEdit" label-width="70px">
                <el-form-item label="名称" prop="name">
                    <el-input v-model="form.name"></el-input>
                </el-form-item>
                <el-form-item label="含义" prop="meaning">
                    <el-input v-model="form.meaning"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="addVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveAdd">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import api from '../../../api/index';
import sysdict from '../../../api/sysdict';
import pageUtil from '../../../utils/page';

export default {
    name: 'basetable',
    data() {
        return {
            query: {
                search_text: '',
                sort_param: '',
                sort_by: '',
                page: 1,
                page_size: 10,
                sift_type: ''
            },
            tableData: [
                {
                    name: 'job',
                    meaning: '工作招聘',
                    is_table: true
                }
            ],
            multipleSelection: [],
            delList: [],
            editVisible: false,
            pageTotal: 0,
            form: {},
            idx: -1,
            id: -1,
            addVisible: false,
            roles: [],
            rulesEntityAdd: {
                name: [{ required: true, message: '请输入名称', trigger: 'blur' }],
                meaning: [{ required: true, message: '请输入含义', trigger: 'blur' }]
            },
            rulesEntityEdit: {
                meaning: [{ required: true, message: '请输入含义', trigger: 'blur' }]
            },
            sysdict,
            allowVisible: false,
            allowBatchVisible: false,
            tableVisible: false,
            tableBatchVisible: false,
            algorithmParams: [
                {name: '', web_url: '', start_url: ''}
            ],
            goalVisible: false,
            statistics: {},
            loading: false,
        };
    },
    created() {
        this.getData();
    },
    methods: {
        handleAssignTotal() {
            pageUtil.gotoPage(this, 'assign');
        },
        handleAssignNum() {
            pageUtil.gotoPage(this, 'assign', {sift_assign: true});
        },
        handleUnassignNum() {
            pageUtil.gotoPage(this, 'assign', {sift_assign: false});
        },
        // 获取 easy-mock 的模拟数据
        getData() {
            this.loading = true;
            api.getCommentList(this.query).then(res => {
                this.tableData = res.records;
                this.pageTotal = res.total;
                // this.statistics = res.statistics_datas;
                this.loading = false;
            }).catch(err => {
                this.$message.error(err);
                this.loading = false;
            });
        },
        // 触发搜索按钮
        handleSearch() {
            this.$set(this.query, 'page', 1);
            this.getData();
        },
        // 删除操作
        handleDelete(index, row) {
            // 二次确认删除
            this.$confirm('确定要删除吗？', '提示', {
                type: 'warning'
            })
            .then(() => {
                this.loading = true;
                api.deleteFeedback({id: row.id}).then(res => {
                    this.$message.success('删除成功');
                    this.tableData.splice(index, 1);
                    this.loading = false;
                    this.getData();
                }).catch(err => {
                    this.$message.error(err);
                    this.loading = false;
                });
            })
            .catch(() => {});
        },
        // 多选操作
        handleSelectionChange(val) {
            this.multipleSelection = val;
        },
        // 目标操作
        handleGoal(index, row) {
            this.idx = index;
            this.form = row;
            this.goalVisible = true;
        },
        // 保存目标
        saveGoal() {
            this.goalVisible = false;
        },
        // 编辑操作
        handleEdit(index, row) {
            this.idx = index;
            this.form = row;
            this.editVisible = true;
        },
        // 保存编辑
        saveEdit() {
            this.$refs.entityEdit.validate(valid => {
                if (valid) {
                    api.updateDataType(Object.assign({}, this.form)).then(res => {
                        this.editVisible = false;
                        this.$message.success(`修改第 ${this.idx + 1} 行成功`);
                        this.$set(this.tableData, this.idx, this.form);
                        this.getData();
                    }).catch(err => {
                        this.$message.error(err);
                    });
                } else {
                    return false;
                }
            });
        },
        // 新增操作
        handleAdd() {
            this.form = {};
            this.addVisible = true;
        },
        saveAdd() {
            this.$refs.entityAdd.validate(valid => {
                if (valid) {
                    api.addDataType(Object.assign({}, this.form)).then(res => {
                        this.$message.success(`新增成功`);
                        this.addVisible = false;
                        this.getData();
                    }).catch(err => {
                        this.$message.error(err);
                    });
                } else {
                    return false;
                }
            });
        },
        // 分页导航
        handlePageChange(val) {
            this.$set(this.query, 'page', val);
            this.getData();
        },
        handleSizeChange(val) {
            this.$set(this.query, 'page_size', val);
            this.getData();
        },
        changedSort(val) {
            console.log(val);
            this.query.sort_by = val.order === null ? '' : val.order;
            this.query.sort_param = val.order === null ? '' : val.prop;
            this.getData();
        },
        handleAllow(index, row) {
            this.idx = index;
            this.form = row;
            this.allowVisible = true;
        },
        saveAllow() {
            api.updateDataType(this.form).then(res => {
                this.$message.success(`审核操作成功`);
                this.$set(this.tableData, this.idx, this.form);
                this.allowVisible = false;
                this.getData();
            }).catch(err => {
                this.$message.error(err);
            });

        },
        handleBatchAllow() {
            const length = this.multipleSelection.length;
            if (length === 0) { return; }
            this.form = {is_allow: false};
            this.allowBatchVisible = true;
        },
        saveBatchAllow() {
            const length = this.multipleSelection.length;
            if (length === 0) { return; }
            let arr = [];
            let arrStr = [];
            this.delList = this.delList.concat(this.multipleSelection);
            for (let i = 0; i < length; i++) {
                arr.push(this.multipleSelection[i].name);
                arrStr.push('"' + this.multipleSelection[i].name + '"');
            }
            
            this.loading = true;
            api.allowBatchDataType({name_list: '[' + arrStr.join(",") + ']', is_allow: this.form.is_allow}).then(res => {
                this.$message.success('批量审核操作成功');
                // var i = 0;
                // while (i < this.tableData.length) {
                //     if (arr.filter((e) => { return e === this.tableData[i].name; }).length > 0) {
                //         this.tableData[i].is_allow = this.form.is_allow;
                //     }
                //     i++;
                // }
                this.loading = false;
                this.allowBatchVisible = false;
                this.getData();
            }).catch(err => {
                this.$message.error(err);
                this.loading = false;
            });
        },
        handleTable(index, row) {
            this.idx = index;
            this.form = row;
            this.tableVisible = true;
        },
        saveTable() {
            api.updateDataType(this.form).then(res => {
                this.$message.success(`设置表格操作成功`);
                this.$set(this.tableData, this.idx, this.form);
                this.tableVisible = false;
            }).catch(err => {
                this.$message.error(err);
            });

        },
        handleBatchTable() {
            const length = this.multipleSelection.length;
            if (length === 0) { return; }
            this.form = {is_allow: false};
            this.tableBatchVisible = true;
        },
        saveBatchTable() {
            const length = this.multipleSelection.length;
            if (length === 0) { return; }
            let arr = [];
            let arrStr = [];
            this.delList = this.delList.concat(this.multipleSelection);
            for (let i = 0; i < length; i++) {
                arr.push(this.multipleSelection[i].name);
                arrStr.push('"' + this.multipleSelection[i].name + '"');
            }
            
            this.loading = true;
            api.tableBatchDataType({name_list: '[' + arrStr.join(",") + ']', is_table: this.form.is_table}).then(res => {
                this.$message.success('批量设置表格操作成功');
                // var i = 0;
                // while (i < this.tableData.length) {
                //     if (arr.filter((e) => { return e === this.tableData[i].name; }).length > 0) {
                //         this.tableData[i].is_table = this.form.is_table;
                //     }
                //     i++;
                // }
                this.loading = false;
                this.tableBatchVisible = false;
                this.getData();
            }).catch(err => {
                this.$message.error(err);
                this.loading = false;
            });
        },
        handleBatchDelete() {
            const length = this.multipleSelection.length;
            if (length === 0) { return; }
            let str = '';
            let arr = [];
            let arrStr = [];
            this.delList = this.delList.concat(this.multipleSelection);
            for (let i = 0; i < length; i++) {
                str += this.multipleSelection[i].name + ' ';
                arr.push(this.multipleSelection[i].name);
                arrStr.push('"' + this.multipleSelection[i].name + '"');
            }
            
            this.$confirm('确定要删除以下内容吗？<br/>' + '<div style="color: #F56C6C;">'+ arr.join(', ') +'</div>', '提示', {
                type: 'warning',
                dangerouslyUseHTMLString: true
            })
            .then(() => {  
                // this.$message.error(`删除了${str}`);
                // this.multipleSelection = [];
                this.loading = true;
                api.deleteBatchDataType({name_list: '[' + arrStr.join(",") + ']'}).then(res => {
                    this.$message.success('批量删除成功');
                    // var i = 0;
                    // while (i < this.tableData.length) {
                    //     if (arr.filter((e) => { return e === this.tableData[i].name; }).length > 0) {
                    //         this.tableData.splice(i, 1);
                    //         i = 0;
                    //         continue;
                    //     }
                    //     i++;
                    // }
                    this.multipleSelection = [];
                    this.pageTotal -= arr.length;
                    this.loading = false;
                    this.getData();
                }).catch(err => {
                    this.$message.error(err);
                    this.loading = false;
                });
            })
            .catch(() => {});
        }
    }
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}

.handle-input {
    width: 300px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 10px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
.algorithm-params {
    border: 1px dashed #ddd;
    padding: 5px;
    text-align: right;
}
</style>
